package org.ah.ssm.ahutils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.ah.ssm.entity.Examinee;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class AhInterceptor extends HandlerInterceptorAdapter {
	// DispatcherServlet完全处理完请求后被调用
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		System.out.println("===========HandlerInterceptor1 preHandle");
		
		
		Examinee eUser = (Examinee) request.getSession().getAttribute(
				AhConst.ATTR_EXAMINEE);
		if (eUser == null) {
			request.getRequestDispatcher("/WEB-INF/Views/V01_login.jsp")
					.forward(request, response);
			return false;
		} else if (AhUtils.whoAnswered.get((eUser.getExaName()))!=null) {
			// 已经答过题
			request.getRequestDispatcher("/WEB-INF/Views/V03_score2.jsp")
					.forward(request, response);
			return false;

		}
		
		return true;
	}

	// 执行:业务处理器处理请求执行完成后,生成视图之前
	@Override
	public void postHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler,
			ModelAndView modelAndView) throws Exception {
		System.out.println("===========HandlerInterceptor1 postHandle");
	}

	// 执行:业务处理器处理请求之前
	@Override
	public void afterCompletion(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception ex)
			throws Exception {
		System.out.println("===========HandlerInterceptor1 afterCompletion");
	}
}
